﻿Imports System
Imports System.DateTime
Imports DotNetNuke.Entities.Users
Imports DotNetNuke.Entities.Portals
Imports DotNetNuke.Entities.Modules
Imports RTP.HTMLNewsletter
Imports DotNetNuke.Common
Imports DotNetNuke.Security.Roles
Imports DotNetNuke.Services.FileSystem
Imports DotNetNuke.Common.Utilities
Imports DataProvider = DotNetNuke.Data.DataProvider
Imports System.IO
Imports System.Reflection
Imports System.Drawing


Namespace ITCrossing

#Region "metaPostProvider Class "

    Public Class metaPostHTMLNewsletterProvider
        Implements ITCrossing.IPublishable

#Region "IPublishable Members"

#Region "Public Properties"
        Public ReadOnly Property SettingsUserControlPath() As String Implements IPublishable.SettingsUserControlPath
            Get
                Return Nothing
            End Get
        End Property

        Public ReadOnly Property ProviderKey() As String Implements IPublishable.ProviderKey
            Get
                Return MetaPostServices.GetFriendlyNameFromModuleDefinition("HTML Newsletter")
            End Get
        End Property

        Public ReadOnly Property LocalizationFilePath() As String Implements IPublishable.LocalizationFilePath
            Get
                Return "/DesktopModules/HTMLNewsLetter/App_LocalResources/metaPostProvider"
            End Get
        End Property

        Public ReadOnly Property AttachmentUploadPath() As String Implements IPublishable.AttachmentUploadPath
            Get
                Return MetaPostServices.GetProviderAttachmentUploadPath(ProviderKey)
            End Get
        End Property

        Public ReadOnly Property ImageUploadPath() As String Implements IPublishable.ImageUploadPath
            Get
                Return MetaPostServices.GetProviderImageUploadPath(ProviderKey)
            End Get
        End Property

        Public ReadOnly Property ManifestFilePath() As String Implements IPublishable.ManifestFilePath
            Get
                Return "/DesktopModules/HTMLNewsLetter/Providers/HTMLNewsletter.metaPostProvider/Manifests/wlwHTMLNewsletter.xml"
            End Get
        End Property
#End Region

#Region "Get Methods"
        Public Function GetItem(ByVal itemId As String, ByVal userInfo As UserInfo, ByVal portalSettings As PortalSettings, ByVal itemType As ItemType) As Item Implements IPublishable.GetItem
            Dim objNewsletterCtl As New NewsletterController
            Dim objNewsletter As NewsletterInfo = objNewsletterCtl.GetNewsletterbyID(CInt(itemId))
            Return CreateItem(objNewsletter, portalSettings)

        End Function

        Public Function GetModulesForUser(ByVal userInfo As UserInfo, ByVal portalSettings As PortalSettings, ByVal providerKey As String) As ModuleInfoStruct() Implements IPublishable.GetModulesForUser
            Return MetaPostServices.GetModulesForUser(providerKey, userInfo.UserID, portalSettings.PortalId)
        End Function

        Public Function GetRecentItems(ByVal moduleLevelId As String, ByVal userInfo As UserInfo, ByVal portalSettings As PortalSettings, ByVal numberofItems As Integer, ByVal requestType As RecentItemsRequestType, ByVal providerKey As String) As Item() Implements IPublishable.GetRecentItems

            Dim arrItems As New ArrayList
            Dim objNewsletterCtl As New NewsletterController

            Dim arrNewsletters As ArrayList = objNewsletterCtl.GetNewslettersForEdit(MetaPostServices.GetModuleIdByTabModuleId(CInt(moduleLevelId)))
            Dim countItems As Integer = 1

            For Each objNewsletter As NewsletterInfo In arrNewsletters
                If objNewsletter.ModuleID <> 0 Then
                    arrItems.Add(CreateItem(objNewsletter, portalSettings))
                    countItems += 1
                    If countItems > numberofItems Then
                        Exit For
                    End If
                End If
            Next

            Return CType(arrItems.ToArray(GetType(Item)), Item())

        End Function

#End Region

#Region "Add, Edit and Delete Methods"
        Public Function DeleteItem(ByVal itemId As String, ByVal userInfo As UserInfo, ByVal portalSettings As PortalSettings, ByVal itemType As ItemType) As Boolean Implements IPublishable.DeleteItem

            Dim objNewsletterCtl As New NewsletterController
            Dim objNewsletter As NewsletterInfo = objNewsletterCtl.GetNewsletterbyID(CInt(itemId))
            If objNewsletter Is Nothing Then
                Return False
            End If

            If objNewsletter.ArticleID > 0 Then
                DeleteVentrian(objNewsletter.ArticleID, userInfo, portalSettings)
            End If

            oNLBase.DeleteNewsletter(CInt(itemId))

            Return True
        End Function

        Public Function EditItem(ByVal moduleLevelId As String, ByVal userInfo As UserInfo, ByVal portalsettings As PortalSettings, ByVal item As Item) As Boolean Implements IPublishable.EditItem
            Dim images As Generic.List(Of String) = MetaPostServices.GetImageListFromItem(item)

            Dim objNewsletterCtl As New NewsletterController
            Dim objNewsletter As NewsletterInfo = objNewsletterCtl.GetNewsletterbyID(CInt(item.ItemId))
            If objNewsletter Is Nothing Then
                Return False
            End If

            Dim objNAArticleInfo As NAArticleInfo = CreateNewsArticle(objNewsletter.ArticleTabModuleID, objNewsletter.ArticleID, item, userInfo, portalsettings)

            Dim mc As New ModuleController
            Dim ModSettings As Hashtable = mc.GetModuleSettings(objNewsletter.ModuleID)

            Dim PublishDate As DateTime = item.DateCreated
            If Not PublishDate.Year > 1 Then
                PublishDate = UtcNow()
            End If

            Dim tmpItem As Item = GetSummaryFromExtendedContent(item)

            oNLBase.UpdateNewsletter(CInt(item.ItemId), ManageUploadDirectory(tmpItem.Summary, portalsettings.HomeDirectory), ManageUploadDirectory(tmpItem.Content, portalsettings.HomeDirectory), item.Title, userInfo.UserID, objNewsletter.ModuleID, objNAArticleInfo.ArticleID, objNAArticleInfo.ArticleTabModuleID, item.Publish, PublishDate, ModSettings)

            'MetaPostServices.SaveItemImages(CInt(item.ItemId), images, ProviderKey, portalsettings, userInfo.UserID)
            SaveItemImages(CInt(item.ItemId), images, ProviderKey, portalsettings, userInfo.UserID)

            'Now that files have been saved, set the correct images in Ventrian News Article Images table
            UpdateVentrianNAImages(objNAArticleInfo.ArticleID)

            Return True
        End Function


        Public Function NewItem(ByVal moduleLevelId As String, ByVal userInfo As UserInfo, ByVal portalsettings As PortalSettings, ByVal item As Item) As String Implements IPublishable.NewItem
            Dim images As Generic.List(Of String) = MetaPostServices.GetImageListFromItem(item)

            Dim itemId As Integer = -1
            Dim NLModuleid As Integer = MetaPostServices.GetModuleIdByTabModuleId(CInt(moduleLevelId))

            item = GetSummaryFromExtendedContent(item)

            Dim objNAArticleInfo As NAArticleInfo = CreateNewsArticle(0, 0, item, userInfo, portalsettings)

            Dim mc As New ModuleController
            Dim ModSettings As Hashtable = mc.GetModuleSettings(NLModuleid)

            Dim PublishDate As DateTime = item.DateCreated
            If Not PublishDate.Year > 1 Then
                PublishDate = UtcNow()
            End If


            itemId = oNLBase.AddNewsletter(ManageUploadDirectory(item.Summary, portalsettings.HomeDirectory), ManageUploadDirectory(item.Content, portalsettings.HomeDirectory), item.Title, userInfo.UserID, NLModuleid, objNAArticleInfo.ArticleID, objNAArticleInfo.ArticleTabModuleID, item.Publish, PublishDate, ModSettings, portalsettings.PortalId)

            'MetaPostServices.SaveItemImages(itemId, images, ProviderKey, portalsettings, userInfo.UserID)
            SaveItemImages(itemId, images, ProviderKey, portalsettings, userInfo.UserID)

            'Now that files have been saved, set the correct images in Ventrian News Article Images table
            UpdateVentrianNAImages(objNAArticleInfo.ArticleID)

            Return CStr(itemId)
        End Function

#End Region

#Region "Category Related Procedures"
        Public Function GetCategories(ByVal moduleLevelId As String, ByVal userInfo As UserInfo, ByVal portalsettings As PortalSettings) As ItemCategoryInfo() Implements IPublishable.GetCategories

            Dim ArrRoles As ArrayList = oNLBase.GetPortalRoles(portalsettings.PortalId)
            Dim ArrCategories As New ArrayList

            Dim ParentId As Integer
            Dim CategoryId As Integer = oNLBase.CategoryEmail
            ParentId = CategoryId
            ArrCategories.Add(CreateCategory(CategoryId, "Email To", 0))


            ArrCategories.Add(CreateCategory(oNLBase.CategoryYourself, strYourself, ParentId))

            ArrCategories.Add(CreateCategory(oNLBase.CategoryRoles, "Roles", ParentId))

            For Each objRole As RoleInfo In ArrRoles
                CategoryId = CInt(oNLBase.RoleToCategoryID(objRole.RoleID))
                ArrCategories.Add(CreateCategory(CategoryId, objRole.RoleName, oNLBase.CategoryRoles))
            Next

            Dim arrVentrianCategories As ArrayList = GetVentrianCategories(userInfo, portalsettings)
            If arrVentrianCategories.Count > 0 And Not arrVentrianCategories Is Nothing Then
                CategoryId = oNLBase.CategoryNewsArticles
                ArrCategories.Add(CreateCategory(oNLBase.CategoryNewsArticles, "Publish To", 0))
                For Each objVentrianCategory As VentrianCategoryInfo In arrVentrianCategories
                    ArrCategories.Add(CreateCategory(objVentrianCategory.CategoryId, objVentrianCategory.CategoryName, objVentrianCategory.ParentId))
                Next
            End If

            Return CType(ArrCategories.ToArray(GetType(ItemCategoryInfo)), ItemCategoryInfo())
        End Function

        Public Function GetItemCategories(ByVal itemId As String, ByVal userInfo As UserInfo, ByVal portalsettings As PortalSettings) As ItemCategory() Implements IPublishable.GetItemCategories
            Dim objNewsletterCtl As New NewsletterController
            Dim objNewsletter As NewsletterInfo = objNewsletterCtl.GetNewsletterbyID(CInt(itemId))
            Dim arrCategories As New ArrayList
            If Not objNewsletter Is Nothing Then
                Dim objNewsletterCategoryCtl As New NewsletterCategoryController
                Dim arrNLCategories As ArrayList = objNewsletterCategoryCtl.GetNewsletterCategorysByNewsletterID(objNewsletter.NewsletterID)
                For Each objNewsletterCategory As NewsletterCategoryInfo In arrNLCategories
                    Dim objCategory As New ItemCategory
                    objCategory.CategoryId = objNewsletterCategory.CategoryID.ToString
                    objCategory.CategoryName = GetCategoryNameFromCategoryID(objCategory.CategoryId, "", userInfo, portalsettings)
                    objCategory.IsPrimary = False
                    arrCategories.Add(objCategory)
                Next
                If objNewsletter.ArticleID > 0 Then
                    Dim arrNACategories As ArrayList = GetVentrianItemCategories(objNewsletter.ArticleID, objNewsletter.ArticleTabModuleID, userInfo, portalsettings)
                    For Each NACategoryID As Integer In arrNACategories
                        Dim objCategory As New ItemCategory
                        objCategory.CategoryId = NACategoryID.ToString
                        objCategory.CategoryName = GetCategoryNameFromCategoryID(objCategory.CategoryId, "", userInfo, portalsettings)
                        objCategory.IsPrimary = False
                        arrCategories.Add(objCategory)
                    Next
                End If
            End If

            Return CType(arrCategories.ToArray(GetType(ItemCategory)), ItemCategory())
        End Function

        Public Function NewCategory(ByVal moduleLevelId As String, ByVal category As NewItemCategory, ByVal userInfo As UserInfo, ByVal portalsettings As PortalSettings) As Integer Implements IPublishable.NewCategory

        End Function

        Public Function SetItemCategories(ByVal itemId As String, ByVal itemCategories() As ItemCategory, ByVal userInfo As UserInfo, ByVal portalsettings As PortalSettings) As Boolean Implements IPublishable.SetItemCategories
            Dim objNewsletterCtl As New NewsletterController
            Dim objNewsletter As NewsletterInfo = objNewsletterCtl.GetNewsletterbyID(CInt(itemId))

            Dim objNewsletterCategoryCtl As New NewsletterCategoryController
            Dim arrNewsletterCategorys As ArrayList = objNewsletterCategoryCtl.GetNewsletterCategorysByNewsletterID(CInt(itemId))
            For Each objNewsletterCategory As NewsletterCategoryInfo In arrNewsletterCategorys
                Dim blDelete As Boolean = True
                For Each Category As ItemCategory In itemCategories
                    If objNewsletterCategory.CategoryID.ToString = Category.CategoryId Then
                        blDelete = False
                    End If
                Next
                If blDelete Then
                    objNewsletterCategoryCtl.DeleteNewsletterCategory(objNewsletterCategory.NewsletterID, objNewsletterCategory.CategoryID)
                End If
            Next

            Dim mc As New ModuleController
            Dim ModSettings As Hashtable = mc.GetModuleSettings(objNewsletter.ModuleID)
            Dim addChar As String = ""
            Dim SendToRoles As String = ""

            arrNewsletterCategorys = Nothing
            arrNewsletterCategorys = objNewsletterCategoryCtl.GetNewsletterCategorysByNewsletterID(CInt(itemId))
            For Each Category As ItemCategory In itemCategories
                If CInt(Category.CategoryId) = oNLBase.CategoryYourself Then
                    oNLBase.SendNewsletterYourself(userInfo.UserID, userInfo.Email, CInt(itemId), objNewsletter.PublishDate)
                ElseIf CInt(Category.CategoryId) > oNLBase.CategoryNewsArticles Then
                ElseIf CInt(Category.CategoryId) > oNLBase.CategoryEmail Then
                    Dim result As Boolean = False
                    Dim roleID As Integer = oNLBase.CategoryIDToRole(Category.CategoryId)
                    result = oNLBase.SendNewsletterRole(ModSettings, portalsettings, roleID, CInt(itemId), objNewsletter.Published, objNewsletter.PublishDate)
                    Dim objRoleController As New RoleController
                    Dim objRole As RoleInfo = objRoleController.GetRole(roleID, portalsettings.PortalId)
                    If Not objRole Is Nothing Then
                        SendToRoles += addChar + objRole.RoleName
                        addChar = ";"
                    End If
                    End If
            Next

            Dim blEnableSocial As Boolean = False
            If CType(ModSettings("enablesocial"), String) <> "" Then
                blEnableSocial = CType(ModSettings("enablesocial"), Boolean)
            End If
            If SendToRoles <> "" And blEnableSocial Then
                Dim displayTabModule As Integer = 0
                If CType(ModSettings("displaytabmodule"), String) <> "" Then
                    displayTabModule = CType(ModSettings("displaytabmodule"), Integer)
                Else
                    Dim objModuleController As New DotNetNuke.Entities.Modules.ModuleController
                    displayTabModule = objModuleController.GetModule(objNewsletter.ModuleID).TabModuleID
                End If

                Dim objNotifications As New Components.Integration.Notifications
                objNotifications.NewNewsletterNotification(SendToRoles.Split(";"c), portalsettings.UserInfo, objNewsletter.NewsletterDesc, objNewsletter.NewsletterID, portalsettings.PortalId, displayTabModule, objNewsletter.ModuleID)
            End If

            Return True
        End Function

#End Region

#End Region

#Region "Private to HTMLNewsletters"
        Private strYourself As String = "Yourself"
        Private oNLBase As New RTP.HTMLNewsletter.NewsletterBase

        Private Function CreateNewsArticle(ByVal inArticleTabModuleID As Integer, ByVal inArticleID As Integer, ByVal item As Item, ByVal userinfo As UserInfo, ByVal portalsettings As PortalSettings) As NAArticleInfo
            Dim objNAArticleInfo As New NAArticleInfo
            objNAArticleInfo.ArticleTabModuleID = 0
            objNAArticleInfo.ArticleID = 0
            Dim NAmoduleLevelID As String = ""
            Dim ArticleTabModuleID As Integer = 0

            Dim NACategories As New ArrayList
            For Each CategoryName As String In item.Categories
                Dim objNACategory As NACategoryInfo = GetNACategoryInfo("", CategoryName, userinfo, portalsettings)
                If ArticleTabModuleID = 0 Or CInt(objNACategory.NAmoduleLevelID) = ArticleTabModuleID Then
                    NAmoduleLevelID = objNACategory.NAmoduleLevelID
                    objNAArticleInfo.ArticleTabModuleID = CInt(objNACategory.NAmoduleLevelID)
                    ArticleTabModuleID = CInt(objNACategory.NAmoduleLevelID)
                    If objNACategory.NACategoryName <> "" Then
                        NACategories.Add(objNACategory.NACategoryName)
                    End If
                End If
            Next
            If ArticleTabModuleID <> 0 Or inArticleTabModuleID <> 0 Then
                Dim objVentrian As New metaPostVentrianNewsArticleProvider
                If ArticleTabModuleID <> inArticleTabModuleID And inArticleTabModuleID > 0 Then
                    DeleteVentrian(inArticleID, userinfo, portalsettings)
                End If
                If ArticleTabModuleID <> 0 Then
                    Dim NAItem As Item = item
                    NAItem.Categories = CType(NACategories.ToArray(GetType(String)), String())
                    If inArticleTabModuleID <> 0 And inArticleTabModuleID = ArticleTabModuleID Then
                        objNAArticleInfo.ArticleID = inArticleID
                        NAItem.ItemId = inArticleID.ToString
                        Dim blNAOK As Boolean = False
                        blNAOK = objVentrian.EditItem(NAmoduleLevelID, userinfo, portalsettings, NAItem)
                    Else
                        objNAArticleInfo.ArticleID = CInt(objVentrian.NewItem(NAmoduleLevelID, userinfo, portalsettings, NAItem))
                    End If
                End If
            End If

            Return objNAArticleInfo
        End Function

        Private Function CreateCategory(ByVal CategoryID As Integer, ByVal CategoryName As String, ByVal ParentId As Integer) As ItemCategoryInfo
            Dim objCategoryInfo As New ItemCategoryInfo

            objCategoryInfo.CategoryId = CategoryID
            objCategoryInfo.CategoryName = CategoryName
            objCategoryInfo.Description = CategoryName
            objCategoryInfo.ParentId = ParentId
            objCategoryInfo.HtmlUrl = ""
            objCategoryInfo.RssUrl = ""

            Return objCategoryInfo
        End Function

        Private Function GetNACategoryInfo(ByVal CategoryID As String, ByVal CategoryName As String, ByVal userInfo As UserInfo, ByVal portalSettings As PortalSettings) As NACategoryInfo
            Dim objNACategory As New NACategoryInfo
            If CategoryID <> "" Then
                objNACategory.CategoryID = CategoryID
                objNACategory.CategoryName = GetCategoryNameFromCategoryID(CategoryID, "", userInfo, portalSettings)
            Else
                objNACategory.CategoryName = CategoryName
                objNACategory.CategoryID = GetCategoryIDFromCategoryName(CategoryName, "", userInfo, portalSettings)
            End If

            If CInt(objNACategory.CategoryID) > oNLBase.CategoryNewsArticles Then
                ' NAmoduleLevelID
                Dim iCategoryID As Integer = CInt(objNACategory.CategoryID)
                iCategoryID = iCategoryID - oNLBase.CategoryNewsArticles
                Dim strCategoryID As String = iCategoryID.ToString
                Dim lenMultiplier As Integer = Len(oNLBase.CategoryNAMultiplier) - 1
                objNACategory.NAmoduleLevelID = Left(strCategoryID, Len(strCategoryID) - lenMultiplier)

                ' NAModuleID
                'objNACategory.NAModuleID = MetaPostServices.GetModuleIdByTabModuleId(CInt(objNACategory.NAmoduleLevelID))

                ' NACategoryID
                objNACategory.NACategoryID = CInt(Mid(objNACategory.CategoryID, Len(objNACategory.CategoryID) - lenMultiplier + 1)).ToString

                ' NACategoryName
                If InStr(objNACategory.CategoryName, "~") > 0 Then
                    objNACategory.NAModuleTitle = Trim(Left(objNACategory.CategoryName, InStr(objNACategory.CategoryName, "~") - 1))
                    objNACategory.NACategoryName = String.Format("({0}) {1}", objNACategory.NACategoryID, Trim(Mid(objNACategory.CategoryName, InStr(objNACategory.CategoryName, "~") + 1)))
                Else
                    objNACategory.NAModuleTitle = objNACategory.CategoryName
                    objNACategory.NACategoryName = ""
                End If
            End If
            Return objNACategory
        End Function

        Private Function GetModuleTitle(ByVal intModuleID As Integer) As String
            Dim CacheKey As String = "HTMLNModuleTitle" & intModuleID.ToString
            Dim ModuleTitle As String = CType(DotNetNuke.Common.Utilities.DataCache.GetCache(CacheKey), String)
            If ModuleTitle Is Nothing Then
                Dim objModuleController As New ModuleController
                Dim objModuleTabs As ArrayList = objModuleController.GetModuleTabs(intModuleID)
                Dim objModuleInfo As ModuleInfo
                Dim intTabID As Integer = 0
                For Each objModuleInfo In objModuleTabs
                    If objModuleInfo.TabID < intTabID Or intTabID = 0 Then
                        ModuleTitle = objModuleInfo.ModuleTitle
                        intTabID = objModuleInfo.TabID
                    End If
                Next
                DotNetNuke.Common.Utilities.DataCache.SetCache(CacheKey, ModuleTitle)
            End If
            Return ModuleTitle
        End Function

        Private Function GetCategoryNameFromCategoryID(ByVal CategoryID As String, ByVal moduleLevelId As String, ByVal userInfo As UserInfo, ByVal portalSettings As PortalSettings) As String
            Dim arrCategories As ItemCategoryInfo() = GetCategories(moduleLevelId, userInfo, portalSettings)
            Dim CategoryName As String = ""
            For Each objCategory As ItemCategoryInfo In arrCategories
                If CategoryID = objCategory.CategoryId.ToString Then
                    CategoryName = objCategory.CategoryName
                End If
            Next
            Return CategoryName
        End Function

        Private Function GetCategoryIDFromCategoryName(ByVal CategoryName As String, ByVal moduleLevelId As String, ByVal userInfo As UserInfo, ByVal portalSettings As PortalSettings) As String
            Dim arrCategories As ItemCategoryInfo() = GetCategories(moduleLevelId, userInfo, portalSettings)
            Dim CategoryID As String = ""
            For Each objCategory As ItemCategoryInfo In arrCategories
                If CategoryName = objCategory.CategoryName.ToString Then
                    CategoryID = objCategory.CategoryId.ToString
                End If
            Next
            Return CategoryID
        End Function

        Private Function CreateItem(ByVal objNewsletter As NewsletterInfo, ByVal portalsettings As PortalSettings) As Item
            Dim objItem As New Item
            objItem.ItemId = objNewsletter.NewsletterID
            objItem.ItemType = ItemType.Post
            objItem.StyleDetectionPost = False
            objItem.StyleId = ""
            objItem.AuthorId = objNewsletter.NewsletterUserID.ToString
            objItem.Link = "~/Default.aspx?tabid=" & portalsettings.ActiveTab.TabID & "&ViewID=" & objNewsletter.NewsletterID
            objItem.Permalink = objItem.Link
            objItem.Publish = objNewsletter.Published
            objItem.ExtendedContent = objNewsletter.NewsletterBody
            objItem.Content = objNewsletter.NewsletterSummary
            objItem.Title = objNewsletter.NewsletterDesc
            objItem.DateCreated = objNewsletter.PublishDate

            Return objItem
        End Function

        Private Function GetSummaryFromExtendedContent(ByVal initem As Item) As Item
            Dim outitem As Item = initem
            If initem.ExtendedContent <> "" And Not initem.ExtendedContent Is Nothing Then
                outitem.Summary = Trim(initem.Content)
                outitem.Content = Trim(initem.ExtendedContent)
            End If
            Return outitem
        End Function

        Private Function UpdateVentrianNAImages(ByVal ArticleID As Integer) As Boolean
            Return oNLBase.UpdateVentrianNAImages(ArticleID)
        End Function
#End Region

#Region "Functions called via reflection"
        Public Function GetVentrianCategories(ByVal userInfo As UserInfo, ByVal portalSettings As PortalSettings) As ArrayList
            Dim objVentrian As New metaPostVentrianNewsArticleProvider
            Dim NAProviderKey As String = objVentrian.ProviderKey
            Dim arrCategories As New ArrayList
            If Not NAProviderKey Is Nothing Then
                Dim arrVentrianModuleInfoStructs() As ModuleInfoStruct
                Dim CategoryId, ParentId As Integer
                Try
                    arrVentrianModuleInfoStructs = objVentrian.GetModulesForUser(userInfo, portalSettings, NAProviderKey)
                    For Each objModuleInfoStruct As ModuleInfoStruct In arrVentrianModuleInfoStructs
                        Dim ModuleID As Integer = MetaPostServices.GetModuleIdByTabModuleId(CInt(objModuleInfoStruct.ModuleLevelId))
                        Dim ModuleTitle As String = GetModuleTitle(ModuleID)
                        CategoryId = CInt(oNLBase.NAModToCategory(objModuleInfoStruct.ModuleLevelId))
                        ParentId = CategoryId
                        Dim objVentrianCategoryInfo As New VentrianCategoryInfo
                        objVentrianCategoryInfo.CategoryId = CategoryId
                        objVentrianCategoryInfo.CategoryName = ModuleTitle
                        objVentrianCategoryInfo.ParentId = oNLBase.CategoryNewsArticles
                        arrCategories.Add(objVentrianCategoryInfo)
                        Dim arrVentrianCategories() As ItemCategoryInfo
                        arrVentrianCategories = objVentrian.GetCategories(objModuleInfoStruct.ModuleLevelId, userInfo, portalSettings)
                        Dim blProcess As Boolean = False
                        For Each objCategoryInfo As ItemCategoryInfo In arrVentrianCategories
                            If blProcess Then
                                CategoryId = CInt(Mid(objCategoryInfo.CategoryName, 2, InStr(objCategoryInfo.CategoryName, ")") - 2)) + ParentId
                                Dim CategoryName As String = Trim(Mid(objCategoryInfo.CategoryName, InStr(objCategoryInfo.CategoryName, ")") + 1))
                                objVentrianCategoryInfo = New VentrianCategoryInfo
                                objVentrianCategoryInfo.CategoryId = CategoryId
                                objVentrianCategoryInfo.CategoryName = ModuleTitle + " ~ " + CategoryName
                                objVentrianCategoryInfo.ParentId = ParentId
                                arrCategories.Add(objVentrianCategoryInfo)
                            End If
                            If objCategoryInfo.CategoryName = "Categories" Then
                                blProcess = True
                            End If
                        Next
                    Next
                Catch
                End Try
            End If
            Return arrCategories
        End Function

        Public Function GetVentrianItemCategories(ByVal ArticleID As Integer, ByVal ArticleTabModuleID As Integer, ByVal userInfo As UserInfo, ByVal portalSettings As PortalSettings) As ArrayList
            Dim arrNACategories As New ArrayList
            Dim objVentrian As New metaPostVentrianNewsArticleProvider
            Dim NAitem As Item = objVentrian.GetItem(ArticleID.ToString, userInfo, portalSettings, ItemType.Post)
            If Not NAitem.Categories Is Nothing Then
                For Each NACategory As String In NAitem.Categories
                    Dim CategoryId As Integer = CInt(Mid(NACategory, 2, InStr(NACategory, ")") - 2)) + CInt(oNLBase.NAModToCategory(ArticleTabModuleID.ToString))
                    arrNACategories.Add(CategoryId)
                Next
            End If
            Return arrNACategories
        End Function

        Public Function DeleteVentrian(ByVal ArticleID As Integer, ByVal userInfo As UserInfo, ByVal portalSettings As PortalSettings) As Boolean
            Dim objVentrian As New metaPostVentrianNewsArticleProvider
            Return objVentrian.DeleteItem(ArticleID.ToString, userInfo, portalSettings, ItemType.Post)
        End Function

        Public Function CreateVentrian(ByVal inArticleTabModuleID As Integer, ByVal inArticleID As Integer, ByVal inCategories() As String, ByVal inContent As String, ByVal inPublish As Boolean, ByVal inSummary As String, ByVal inTitle As String, ByVal userInfo As UserInfo, ByVal portalSettings As PortalSettings) As NAArticleInfo
            Dim item As New Item
            item.ItemId = inArticleID
            item.AllowComments = -1
            item.AllowTrackbacksOrPings = 0
            item.Categories = inCategories
            item.Content = inContent
            item.ItemType = ItemType.Post
            item.Publish = inPublish
            item.StyleDetectionPost = False
            item.StyleId = ""
            item.Summary = inSummary
            item.Title = inTitle
            Return CreateNewsArticle(inArticleTabModuleID, inArticleID, item, userInfo, portalSettings)
        End Function
#End Region

#Region "Temporary"
        Public Shared Sub SaveItemImages(ByVal itemId As Integer, ByVal images As List(Of String), ByVal providerKey As String, ByVal portalSettings As PortalSettings, ByVal UserId As Integer)


            Dim folderPathAndFile As String = String.Empty

            Dim folderPath As String = String.Empty

            Dim fileName As String = String.Empty

            Dim physicalPath As String = String.Empty

            Dim fileId As Integer = 0

            Dim folderId As Integer = 0



            If images.Count > 0 Then


                ' Parse the image information to find the folder and file information

                ' and then create new entries in the DNN Folders and Files tables if 

                ' needed for the new entry.

                Dim folderController As New FolderController()

                Dim folderInfo As FolderInfo = Nothing



                For Each image As String In images


                    Try




                        folderPathAndFile = image.Substring(image.IndexOf(portalSettings.HomeDirectory) + portalSettings.HomeDirectory.Length)
                        folderPathAndFile = Replace(folderPathAndFile, "%20", " ")


                        physicalPath = Convert.ToString(portalSettings.HomeDirectoryMapPath) & folderPathAndFile.Replace("/", "\")

                        ' It turns out that we need to grab this before we save the folder since

                        ' it appears that DNN doesn't release the handle to the folder when it creates

                        ' the folder.

                        Dim imageFileInfo As ImageFileInfo = GetImageFileInformation(physicalPath)

                        fileName = image.Substring(image.LastIndexOf("/") + 1)

                        folderPath = folderPathAndFile.Substring(0, folderPathAndFile.LastIndexOf("/") + 1)

                        folderInfo = folderController.GetFolder(portalSettings.PortalId, folderPath)

                        If folderInfo Is Nothing Then


                            Try


                                folderId = folderController.AddFolder(portalSettings.PortalId, folderPath)

                                ' Set the folder permissions

                                ' Administrators get read and write

                                FileSystemUtils.SetFolderPermissions(portalSettings.PortalId, folderId, portalSettings.AdministratorRoleId, folderPath)

                                ' Logged in user gets read and write

                                Dim miArray As MethodInfo() = GetType(FileSystemUtils).GetMethods()

                                Dim miNewVersion As MethodInfo = Nothing

                                Dim miOldVersion As MethodInfo = Nothing

                                For Each mi As MethodInfo In miArray


                                    If mi.Name = "SetFolderPermission" Then


                                        If mi.GetParameters().Length = 5 Then
                                            miOldVersion = mi
                                        End If

                                        If mi.GetParameters().Length = 6 Then
                                            miNewVersion = mi

                                        End If
                                    End If

                                    If miNewVersion IsNot Nothing Then
                                        Exit For

                                    End If
                                Next

                                If miOldVersion IsNot Nothing OrElse miNewVersion IsNot Nothing Then


                                    Dim mParams As Object() = Nothing

                                    If miNewVersion IsNot Nothing Then


                                        mParams = New Object(5) {}

                                        mParams.SetValue(portalSettings.PortalId, 0)

                                        mParams.SetValue(folderId, 1)

                                        mParams.SetValue(5, 2)

                                        mParams.SetValue(Nothing, 3)

                                        mParams.SetValue(UserId, 4)

                                        mParams.SetValue(folderPath, 5)

                                        miNewVersion.Invoke(Nothing, mParams)

                                        mParams.SetValue(6, 2)


                                        miNewVersion.Invoke(Nothing, mParams)

                                    ElseIf miOldVersion IsNot Nothing Then


                                        mParams = New Object(4) {}

                                        mParams.SetValue(portalSettings.PortalId, 0)

                                        mParams.SetValue(folderId, 1)

                                        mParams.SetValue(5, 2)

                                        mParams.SetValue(-1, 3)

                                        mParams.SetValue(folderPath, 4)

                                        miOldVersion.Invoke(Nothing, mParams)

                                        mParams.SetValue(6, 2)


                                        miOldVersion.Invoke(Nothing, mParams)



                                    End If

                                    'FileSystemUtils.Set(portalSettings.PortalId, folderId, 5, -1, folderPath);

                                End If

                            Catch


                                ' No need to catch this
                            End Try
                        Else




                            folderId = folderInfo.FolderID
                        End If



                        ' By now, we should have a folderId, so we will try to add the image as a new file

                        fileId = AddFile(imageFileInfo, fileName, folderPath, folderId, portalSettings.PortalId)



                        'ImagePath, FileName, FolderPath, FolderI



                        '#Region "Add an entry in the itcMetaPostImages table."



                        Dim SQL As String = "itcMetaPost_Insert_Image"



                        Dim methodParams As Object() = {fileId, itemId, providerKey, UserId}




                        '#End Region

                        DataProvider.Instance().ExecuteNonQuery(SQL, methodParams)

                    Catch


                        'Do nothing since this may just be a result of another process accessing the file.
                    End Try
                    ' End foreach (string image in images)
                Next
            End If
            ' End if (images.Count > 0)
        End Sub



        Private Shared Function AddFile(ByVal imageFileInfo As ImageFileInfo, ByVal fileName As String, ByVal folderPath As String, ByVal folderId As Integer, ByVal portalId As Integer) As Integer


            Dim fileId As Integer = 0



            Dim fileController As New FileController()



            Try










                fileId = fileController.AddFile(portalId, fileName, imageFileInfo.Extension, imageFileInfo.Size, imageFileInfo.Width, imageFileInfo.Height, _
                 imageFileInfo.ContentType, folderPath, folderId, False)

            Catch ex As Exception



                DotNetNuke.Services.Exceptions.Exceptions.LogException(ex)
            End Try



            Return fileId

        End Function





        Private Shared Function GetImageFileInformation(ByVal physicalPath As String) As ImageFileInfo


            Dim imageFileInfo As New ImageFileInfo()

            imageFileInfo.Extension = "png"

            imageFileInfo.ContentType = "image/png"

            imageFileInfo.Size = Null.NullInteger

            imageFileInfo.Width = Null.NullInteger

            imageFileInfo.Height = Null.NullInteger



            Try



                Dim imageStream As Stream = New FileStream(physicalPath, FileMode.Open, FileAccess.Read, FileShare.Read)

                Dim imgImage As Image = Image.FromStream(imageStream)

                imageFileInfo.Height = imgImage.Height

                imageFileInfo.Width = imgImage.Width

                imageFileInfo.Size = imageStream.Length

                imageStream.Close()

                imageFileInfo.Extension = physicalPath.Substring(physicalPath.LastIndexOf(".") + 1)

                If imageFileInfo.Extension <> "png" Then

                    imageFileInfo.ContentType = FileSystemUtils.GetContentType(imageFileInfo.Extension)

                End If

            Catch ex As Exception
                Dim q As Integer = 1
            End Try



            Return imageFileInfo

        End Function
#End Region

#Region "NACategoryInfo"
        Private Class NACategoryInfo
            Private _CategoryName As String
            Private _CategoryID As String
            Private _NAmoduleLevelID As String
            Private _NACategoryID As String
            Private _NACategoryName As String
            Private _NAModuleTitle As String

            Property CategoryName() As String
                Get
                    Return _CategoryName
                End Get
                Set(ByVal Value As String)
                    _CategoryName = Value
                End Set
            End Property

            Property CategoryID() As String
                Get
                    Return _CategoryID
                End Get
                Set(ByVal Value As String)
                    _CategoryID = Value
                End Set
            End Property

            Property NAmoduleLevelID() As String
                Get
                    Return _NAmoduleLevelID
                End Get
                Set(ByVal Value As String)
                    _NAmoduleLevelID = Value
                End Set
            End Property

            Property NACategoryName() As String
                Get
                    Return _NACategoryName
                End Get
                Set(ByVal Value As String)
                    _NACategoryName = Value
                End Set
            End Property

            Property NACategoryID() As String
                Get
                    Return _NACategoryID
                End Get
                Set(ByVal Value As String)
                    _NACategoryID = Value
                End Set
            End Property

            Property NAModuleTitle() As String
                Get
                    Return _NAModuleTitle
                End Get
                Set(ByVal Value As String)
                    _NAModuleTitle = Value
                End Set
            End Property

        End Class
#End Region

#Region "NAArticleInfo"
        Public Class NAArticleInfo
            Private _ArticleTabModuleID As Integer
            Private _ArticleID As Integer


            Property ArticleTabModuleID() As Integer
                Get
                    Return _ArticleTabModuleID
                End Get
                Set(ByVal Value As Integer)
                    _ArticleTabModuleID = Value
                End Set
            End Property

            Property ArticleID() As Integer
                Get
                    Return _ArticleID
                End Get
                Set(ByVal Value As Integer)
                    _ArticleID = Value
                End Set
            End Property


        End Class
#End Region

#Region "VentrianCategoryInfo"
        Private Class VentrianCategoryInfo
            Private _CategoryId As Integer
            Private _CategoryName As String
            Private _ParentId As Integer

            Public Property CategoryId() As Integer
                Get
                    Return _CategoryId
                End Get
                Set(ByVal value As Integer)
                    _CategoryId = value
                End Set
            End Property

            Public Property CategoryName() As String
                Get
                    Return _CategoryName
                End Get
                Set(ByVal value As String)
                    _CategoryName = value
                End Set
            End Property

            Public Property ParentId() As Integer
                Get
                    Return _ParentId
                End Get
                Set(ByVal value As Integer)
                    _ParentId = value
                End Set
            End Property

        End Class
#End Region
    End Class

#End Region

End Namespace